package org.techmore.activity;

import java.io.File;

import org.techmore.R;
import org.techmore.biz.WelcomeActivityDataHandler;
import org.techmore.utils.DownloadHandler;
import org.techmore.utils.HandlerThreadUtils;
import org.techmore.utils.InitailDatabase;
import org.techmore.utils.PropertiesUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class WelcomeActivity extends Activity {
	
	private AlertDialog dialog;
	
	private Handler handler = null;
	private Handler progressHandler = null;
	
	private PropertiesUtil propertiesUtil = PropertiesUtil.getInstance();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Window window = getWindow();
		window.requestFeature(Window.FEATURE_NO_TITLE);
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		setContentView(R.layout.techmore_welcome);
		
		//重置登入狀體
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean("isLogin", false);
		editor.commit();
		
		//初始化数据库
		InitailDatabase.getInstance().initDB(this);
		
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

		View dialogView = LayoutInflater.from(this).inflate(R.layout.techmore_welcome_pop, null);
		dialogBuilder.setView(dialogView);

		final Button welcome_yes = (Button) dialogView.findViewById(R.id.welcome_yes);
		Button welcome_no = (Button) dialogView.findViewById(R.id.welcome_no);
		final ProgressBar welcome_dowmload_progressbar = (ProgressBar) dialogView.findViewById(R.id.welcome_dowmload_progressbar);
		
		dialog = dialogBuilder.create();
		handler = new Handler(Looper.myLooper());
		
		handler.postDelayed(new Runnable() {
			public void run() {
				dialog.show();
			}
		}, 200);
		
		
		progressHandler = new Handler(HandlerThreadUtils.getHandlerThreadInstance().getLooper()){
			public void handleMessage(android.os.Message msg) {
				int signNum = Integer.valueOf(msg.obj.toString());
				if(-1 == signNum){
					//download complete
					File xmlFile = new File(Environment.getDataDirectory()+propertiesUtil.getValue("qr_xml_file_cache")
							+"/"+propertiesUtil.getValue("qr_servicelist_filename"));
					WelcomeActivityDataHandler.getInstance().insertXmlToSqlite(xmlFile,WelcomeActivity.this);
					
					WelcomeActivity.this.startActivity(new Intent(WelcomeActivity.this, CouponListActivity.class));
					
					dialog.dismiss();
//					WelcomeActivity.this.finish();
				}else if(-2 == signNum){
					//download error
				}else{
//					System.out.println("signNum:"+signNum);
					welcome_dowmload_progressbar.setProgress(signNum);
				}
				
				
			};
		};
		
		

		welcome_no.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(WelcomeActivity.this,MyCouponListActivity.class);
				dialog.dismiss();
				WelcomeActivity.this.finish();
				WelcomeActivity.this.startActivity(intent);
			}
		});
		
		welcome_yes.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				welcome_yes.setEnabled(false);
				welcome_dowmload_progressbar.setVisibility(View.VISIBLE);
				progressHandler.post(new DownloadHandler(PropertiesUtil.getInstance().getValue("qr_servicelist_url"), 
						progressHandler, PropertiesUtil.getInstance().getValue("qr_servicelist_filename")));
			}
		});
		
		
		
		//welcome_version初始化版本
		TextView welcome_version = (TextView) this.findViewById(R.id.welcome_version);
		initVersion(welcome_version);
		
		
	}
	
	
	private void initVersion(TextView welcome_version) {
		try {
			PackageInfo packageInfo = getPackageManager().getPackageInfo("org.techmore", PackageManager.GET_CONFIGURATIONS);
			welcome_version.setText(packageInfo.versionName);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
